package com.heima.media.listener;

import com.heima.media.entity.WmNews;
import com.heima.media.service.IAuditSrevice;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.concurrent.TimeUnit;
@Service
public class PublishListener {
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private IAuditSrevice auditSrevice;
//    实例化后自动执行该方法
    @PostConstruct
    public void init(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                RBlockingQueue<Object> queue = redissonClient.getBlockingQueue("article-publish");
                RDelayedQueue<Object> delayedQueue = redissonClient.getDelayedQueue(queue);
                delayedQueue.offer("init",1, TimeUnit.SECONDS);
                while (true){
                    try {
                        Object msg = queue.poll(1, TimeUnit.MINUTES);
                        if(msg!=null&&!msg.equals("init")){
                            System.out.println("接收到消息："+msg+new Date());
                            auditSrevice.audit((WmNews) msg);
                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
